package com.example.rabbitmq.topic.service;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Auther:
 * @Description:
 * @Date: 2019/12/27 11:32
 */
@Service
public class TopicSendMsgService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    // routingKey topic.man
    public String topicSendMan(String message) {
        try {
            message += LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"));
            System.out.println("topicSendMan 发送端发送的 message 是：  " + message);
            rabbitTemplate.convertAndSend("myTopicExchange", "topic.man", message);
            return message;
        } catch (Exception e) {
            return "fail";
        }
    }

    // routingKey topic.man
    public String topicSendWoman(String message) {
        try {
            message += LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"));
            System.out.println("topicSendWoman 发送端发送的 message 是：  " + message);
            rabbitTemplate.convertAndSend("myTopicExchange", "topic.man", message);
            return message;
        } catch (Exception e) {
            return "fail";
        }
    }

    // routingKey topic.man
    public String topicSendAll(String message) {
        try {
            message += LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"));
            System.out.println("topicSendAll 发送端发送的 message 是：  " + message);
            rabbitTemplate.convertAndSend("myTopicExchange", "topic.3433502", message);
            return message;
        } catch (Exception e) {
            return "fail";
        }
    }
}
